


public class MyLinkList implements IList {

    static class LinstNode{
        public int val;
        public LinstNode  nextNode;

        public LinstNode(int val){
            this.val=val;
        }
    }

    public LinstNode head;
    public void creatNode(){
        LinstNode node1 =new LinstNode(13);
        LinstNode node2 =new LinstNode(44);
        LinstNode node3 =new LinstNode(66);
        LinstNode node4 =new LinstNode(77);

        node1.nextNode=node2;
        node2.nextNode=node3;
        node3.nextNode=node4;

        this.head=node1;
    }

    @Override
    public void display() {
        LinstNode cur =head;
        while(cur!=null){
            System.out.println(cur.val);
            cur=cur.nextNode;

        }
    }
    public int size(){
        int len=0;
        LinstNode cur=head;
        while (cur!=null){
            cur=cur.nextNode;
            len++;
        }
        return len;
    }

    @Override
    public void addFirst(int pos) {
        LinstNode node=new LinstNode(pos);

        if(head.nextNode==null){
            head.nextNode=node;
            return;
        }
        LinstNode cur=head;
        head.nextNode=node;
        node.nextNode=cur.nextNode;

    }

    @Override
    public void addLast(int pos) {
        LinstNode node=new LinstNode(pos);
        if(head ==null){
            head=node;
            return;
        }
        LinstNode cur=head;
        while(cur.nextNode!=null){
            cur=cur.nextNode;
        }
        cur.nextNode=node;


    }

    @Override
    public void remove(int key) {
        if (head==null){
            return;
        }
        if (head.val==key){
            head=head.nextNode;
            return;
        }
        LinstNode cur=findNode(key);
        if ((cur==null)){
            return;
        }

        LinstNode del=cur.nextNode;
        cur.nextNode=del.nextNode;
    }

    @Override
    public void removeallkey(int key) {
        if(head==null){
            return;
        }
        LinstNode pre=head;
        LinstNode cur=head.nextNode;
        while (cur!=null){
            if (cur.val==key){
                pre.nextNode=cur.nextNode;
                cur =cur.nextNode;
            }else{
                pre=cur;
                cur=cur.nextNode;
            }

        }
        if (head.val==key){
            head=head.nextNode;
        }

    }

    private LinstNode findNode(int key){
        LinstNode cir=head;
        while ((cir.nextNode != null)) {
            if (cir.nextNode.val==key){
                return cir;
            }
            cir=cir.nextNode;
        }
        return null;
    }

    @Override
    public void addindex(int pos, int date) {
        if(pos<0||pos>size()){
            System.out.println("位置不合法");
            return;

        }
        if(pos==0){
            addFirst(date);
            return;
        }
        if(pos==size()){
            addLast(date);
            return;
        }
        LinstNode cur=head;
        for (int i = 0; i < pos-1; i++) {
            cur=cur.nextNode;

        }



        LinstNode node=new LinstNode(date);
        node.nextNode=cur.nextNode;
        cur.nextNode=node;
    }
}
